package gui;

import javax.swing.*;

import main.DoubleArrayGrid;
import main.FloatArrayGrid;
import main.FloatObjArrayGrid;
import main.TwoDGrid;
import main.DoubleObjectGrid;

import java.awt.*;
import java.awt.event.*;
import java.util.Date;

/**
 *
 * @author Arwa Tyebkhan
 */
public class UserControls extends JPanel implements ActionListener {

  private final int MAX_SIZE = 200;
  private final int MAX_COUNT = 5000;

  private HeatPad heatPad;
  private JCheckBox animationControl;
  private JTextField counter;
  private JButton goButton;
  private JSpinner sizeControl;
  private JSpinner countControl;

  private JRadioButton daPrec;
  private JRadioButton faPrec;
  private JRadioButton floatPrec;
  private JRadioButton doPrec;  

  /** Creates a new instance of UserControls */
  public UserControls(HeatPad heatPad) {
    this.setLayout(new GridLayout(5, 1));
    this.setSize(150, 400);
    this.heatPad = heatPad;

    addPrecisionChoice();
    addSizeContol();
    addIterationControl();
    animationControl = new JCheckBox("Animate");
    animationControl.setSelected(false);
    add(animationControl);
    
//    counter = new JTextField("0");
//    counter.setEnabled(false);
//    add(counter);
    
    addGo();
  }

  private void addPrecisionChoice() {
    JPanel precisionPanel = new JPanel();
    precisionPanel.setSize(150, 100);
    precisionPanel.setLayout(new GridLayout(5, 1));

    JLabel label = new JLabel("Select a simulator type");

    daPrec = new JRadioButton("Array of doubles");
    daPrec.addActionListener(this);
    daPrec.setSelected(true);

    faPrec = new JRadioButton("Array of floats");
    faPrec.addActionListener(this);
    faPrec.setSelected(false);

    floatPrec = new JRadioButton("Array of Floats");
    floatPrec.addActionListener(this);
    floatPrec.setSelected(false);

    doPrec = new JRadioButton("Linked objects");
    doPrec.addActionListener(this);
    doPrec.setSelected(false);

    ButtonGroup precisionGroup = new ButtonGroup();
    precisionGroup.add(daPrec);
    precisionGroup.add(faPrec);
    precisionGroup.add(floatPrec);
    precisionGroup.add(doPrec);

    precisionPanel.add(label);
    precisionPanel.add(daPrec);
    precisionPanel.add(faPrec);
    precisionPanel.add(floatPrec);
    precisionPanel.add(doPrec);
    add(precisionPanel);
    precisionPanel.setLocation(10, 10);
  }

  private void addGo() {
    goButton = new JButton(Util.getImage("go.jpg"));
    goButton.addActionListener(this);
    goButton.setPreferredSize(new Dimension(10, 10));
    add(goButton);
  }

  private void addSizeContol() {
    JPanel sizePanel = new JPanel(new GridLayout(4, 1));

    sizeControl = new JSpinner(new SpinnerNumberModel(100, 1, MAX_SIZE, 1));
    sizeControl.setSize(50, 50);
    sizePanel.add(new JLabel("Set Grid Size"));
    sizePanel.add(sizeControl);

    add(sizePanel);
  }

  private void addIterationControl() {
    JPanel countPanel = new JPanel(new GridLayout(4, 1));

    countControl= new JSpinner(new SpinnerNumberModel(500, 1, MAX_COUNT, 1));
    countControl.setSize(50, 50);
    countPanel.add(new JLabel("Set max # of iterations"));
    countPanel.add(countControl);

    add(countPanel);
  }

  public void actionPerformed(ActionEvent event) {
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    if (event.getSource().equals(goButton)) {
      TwoDGrid grid = null;            
      int dimension = Integer.parseInt(sizeControl.getValue().toString());
      int maxCount = Integer.parseInt(countControl.getValue().toString());

      if(daPrec.isSelected()) {
        DoubleArrayGrid daGrid = new DoubleArrayGrid(dimension, dimension);
        daGrid.initSideValue(TwoDGrid.SIDE_BOTTOM, heatPad.getBottomTemperature());
        daGrid.initSideValue(TwoDGrid.SIDE_LEFT, heatPad.getLeftTemperature());
        daGrid.initSideValue(TwoDGrid.SIDE_RIGHT, heatPad.getRightTemperature());
        daGrid.initSideValue(TwoDGrid.SIDE_TOP, heatPad.getTopTemperature());
        grid = daGrid;
      }
      else if(faPrec.isSelected()) {
        FloatArrayGrid faGrid = new FloatArrayGrid(dimension, dimension);
        faGrid.initSideValue(TwoDGrid.SIDE_BOTTOM, heatPad.getBottomTemperature());
        faGrid.initSideValue(TwoDGrid.SIDE_LEFT, heatPad.getLeftTemperature());
        faGrid.initSideValue(TwoDGrid.SIDE_RIGHT, heatPad.getRightTemperature());
        faGrid.initSideValue(TwoDGrid.SIDE_TOP, heatPad.getTopTemperature());
        grid = faGrid;
      }
      else if(floatPrec.isSelected()) {
        FloatObjArrayGrid faObjGrid = new FloatObjArrayGrid(dimension, dimension);
        faObjGrid.initSideValue(TwoDGrid.SIDE_BOTTOM, new Float(heatPad.getBottomTemperature()));
        faObjGrid.initSideValue(TwoDGrid.SIDE_LEFT, new Float(heatPad.getLeftTemperature()));
        faObjGrid.initSideValue(TwoDGrid.SIDE_RIGHT, new Float(heatPad.getRightTemperature()));
        faObjGrid.initSideValue(TwoDGrid.SIDE_TOP, new Float(heatPad.getTopTemperature()));
        grid = faObjGrid;
      }
      else if(doPrec.isSelected()) {
        long start = new Date().getTime();
          DoubleObjectGrid doGrid = new DoubleObjectGrid(dimension, dimension);
          long end = new Date().getTime();
          double runTime = ((end-start) / 1000d);
//        System.out.println("Run time: "+runTime+" secs");
          doGrid.initSideValue(TwoDGrid.SIDE_BOTTOM, heatPad.getBottomTemperature());
          doGrid.initSideValue(TwoDGrid.SIDE_LEFT, heatPad.getLeftTemperature());
          doGrid.initSideValue(TwoDGrid.SIDE_RIGHT, heatPad.getRightTemperature());
          doGrid.initSideValue(TwoDGrid.SIDE_TOP, heatPad.getTopTemperature());
          grid = doGrid;
      }
      
      heatPad.setHeatGridLattice(dimension, maxCount);
      heatPad.run(grid, animationControl.isSelected(), counter);
    }
    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  }
}